<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/6/13
 * Time: 上午10:31
 */
namespace app\src\invite\logic;

use app\src\account\enmu\AccountEnmu;
use app\src\base\logic\BaseLogic;
use app\src\filiation\logic\FiliationLogic;
use app\src\invite\model\InviteCode;
use app\src\platform\logic\PlatformLogic;
use app\src\seller\logic\SellerLogic;
use think\Exception;

class InviteCodeLogic extends BaseLogic{

    public function _init()
    {
        $this->setModel(new InviteCode());
    }

    /**
     * 检查邀请码以及邀请单位是否正常
     * @param $invite_code
     */
    public function check($invite_code)
    {

        if($invite_code == 'itboye') return $this->apiReturnSuc('success');

        //验证查询邀请码所属
        $map = ['invite_code' => $invite_code];
        $result = $this->getInfo($map);
        if(!$result['status']){
            return $this->apiReturnErr($result['info']);
        }

        if(empty($result['info'])){
            //邀请码不存在
            return $this->apiReturnErr('邀请码错误');
        }

        //验证邀请单位
        $type = $result['info']['type'];
        $account_id = $result['info']['account_id'];

        switch ($type){
            case AccountEnmu::USER :
                //在filiation表中查用户，没有就报错
                $result = (new FiliationLogic)->getInfo(['uid' => $account_id]);
                if(!$result['status']) return $this->apiReturnErr($result['info']);
                if(empty($result['info'])) return $this->apiReturnErr('error: empty user');
                break;
            case AccountEnmu::PLATFORM :
                $result = (new PlatformLogic)->getPlatformInfo($account_id);
                if(!$result['status']) return $this->apiReturnErr('error platform');
                $result = (new PlatformLogic)->getParents($account_id);
                if(!$result['status']) return $this->apiReturnErr($result['info']);
                if(empty($result['info'])) return $this->apiReturnErr('error: error platform');
                break;
            case AccountEnmu::SELLER :
                //查询商家所属平台
                $result = (new SellerLogic)->getSellerInfo($account_id);
                if(!$result['status']) return $this->apiReturnErr($result['info']);
                if(empty($result['info'])) return $this->apiReturnErr('error: error seller');
                break;
            default:
                return $this->apiReturnErr('error: no this type');
        }
        return $this->apiReturnSuc('success');

    }

    /**
     * 创建邀请码
     * @param $type
     * @param $account_id
     */
    public function create($type, $account_id)
    {
        if(!in_array($type, [AccountEnmu::USER, AccountEnmu::PLATFORM, AccountEnmu::SELLER])){
            return $this->apiReturnErr('error account type');
        }

        //检查是否已有邀请码
        $map = ['type' => $type, 'account_id' => $account_id];
        $result = $this->getInfo($map);
        if(!$result['status']) return $this->apiReturnErr($result['info']);
        if(!empty($result['info'])) return $this->apiReturnSuc('success');

        $invite_code = '0'.strval($type).get_36HEX(intval($account_id)+100000);
        $entity = [
            'type' => $type,
            'account_id' => $account_id,
            'invite_code' => $invite_code
        ];
        $result = $this->add($entity);

        if($result['status']){
            return $this->apiReturnSuc('success');
        }else{
            return $this->apiReturnErr($result['info']);
        }

    }

    /**
     * 获取邀请码
     * @param $type
     * @param $account_id
     * @return array
     */
    public function getInviteCode($type, $account_id)
    {
        if(!in_array($type, [AccountEnmu::USER, AccountEnmu::PLATFORM, AccountEnmu::SELLER])){
            return $this->apiReturnErr('error account type');
        }

        $map = ['type' => $type, 'account_id' => $account_id];
        $result = $this->getInfo($map);

        if(empty($result['info'])) return $this->apiReturnErr('error account');

        $invite = $result;
        $invite_code = $invite['info']['invite_code'];
        $result = $this->check($invite_code);
        if(!$result['status']) return $this->apiReturnErr($result['info']);

        return $invite;
    }
}